
package Quotation;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** 
 * @author yanchuanfeng
 * @version 创建时间：2022年2月28日 上午10:02:24 
 *  
 */

public class QuotationCacheManager {
	//行情缓存
    private Map<String, Quotation> quotationMap = new ConcurrentHashMap<String, Quotation>(); //用于存放行情信息
    public static QuotationCacheManager cache = new QuotationCacheManager();
    
    private QuotationCacheManager(){
    	
    }
    
    public static QuotationCacheManager getQuotationCacheInstance(){
    	if(cache == null){
    		cache = new QuotationCacheManager();
    	}
    	return cache;
    }
    
    public Quotation getQuotation(String exchId, String stkId) throws Exception {
        return getQuotation(exchId, stkId, false);
    }
    
    
    /**
     * 获取实时证券行情对象的实例
     */
    public Quotation getQuotation(String exchId, String stkId, boolean isThrowException) throws Exception {

        Quotation quota = quotationMap.get(exchId + stkId);

        if (quota == null && isThrowException) {
            String[] strArray = new String[2];
            strArray[0] = exchId;
            strArray[1] = stkId;
            throw new Exception("获取行情失败");
        }
        return quota;
    }

    /**
     * 向行情内存中增加一个行情对象
     * @param key 主键，exchId+stkId
     * @param quot Quotation对象
     */
    public void setQuotation(Quotation quotation) {
        quotationMap.put(quotation.getPrimaryKey(), quotation);
    } 
}
